/******************************************************************************
*       使用 1000Hz(1ms) 扫描六只数显管
*******************************************************************************/

module dynamicScan #(
    parameter   SYS_CLK = 50_000_000
)(
    input   wire        clk      ,
    input   wire        rstn     ,

    input   wire [7:0]  hourUpper,
    input   wire [7:0]  hourLow  ,
    input   wire [7:0]  minUpper ,
    input   wire [7:0]  minLow   ,
    input   wire [7:0]  secUpper ,
    input   wire [7:0]  secLow   ,

    output  reg  [7:0]  digDisp  ,
    output  reg  [5:0]  sel      
);

localparam  CNT_MAX = SYS_CLK / 1000;

reg  [15:0]     cnt;
reg  [ 2:0]     sel_cnt;

always @(posedge clk or negedge rstn) begin
    if (~rstn)
        cnt     <=  16'd0;
    else if (cnt == CNT_MAX - 1)
        cnt     <=  16'd0;
    else
        cnt     <=  cnt + 1'b1;
end

always @(posedge clk or negedge rstn) begin
    if (~rstn)
        sel_cnt  <=  3'd0;
    else if (sel_cnt == 3'd5 && cnt == CNT_MAX - 1)
        sel_cnt  <=  3'd0;
    else if (cnt == CNT_MAX - 1)
        sel_cnt  <=  sel + 1'b1;
end

always @* begin
    case (sel_cnt)
        3'd0: begin
            digDisp = hourUpper;
            sel     = 6'b011_111;
        end
        3'd1: begin
            digDisp = hourLow  ;
            sel     = 6'b101_111;
        end
        3'd2: begin
            digDisp = minUpper ;
            sel     = 6'b110_111;
        end
        3'd3: begin
            digDisp = minLow   ;
            sel     = 6'b111_011;
        end
        3'd4: begin
            digDisp = secUpper ;
            sel     = 6'b111_101;
        end
        3'd5: begin
            digDisp = secLow   ;
            sel     = 6'b111_110;
        end
        default: sel = 6'b111_111;
    endcase
end
endmodule
